local UIBase = require("view.UIBase")
local PopupEquipmentUp = class("PopupEquipmentUp", UIBase)
local EquipDataHelper = require("data.helper.EquipDataHelper")
local UserDataHelper = require("data.helper.UserDataHelper")
local TypeHelper = require("utils.TypeHelper")
local DataConst = require("const.DataConst")
local TextHelper = require("utils.TextHelper")

function PopupEquipmentUp:ctor()
    local resource = {
        path = "Prefabs/Popup",
        name = "PopupEquipmentUp",
        canvas = 3
    }
    PopupEquipmentUp.super.ctor(self, resource)
end

function PopupEquipmentUp:awake()
    self._btnOne.onClick:AddListener(handler(self, self._onBtnOneClick))
    self._btnFive.onClick:AddListener(handler(self, self._onBtnFiveClick))
    self._btnClose.onClick:AddListener(handler(self, self._onBtnCloseClick))
end

function PopupEquipmentUp:updateUI(unit)
    self._unit = unit
    local config = self._unit:getConfig()

    local mainAttr, subAttr = EquipDataHelper.getEquipmentAttr(self._unit)
    local mainName, mainValue = TextHelper.getAttrBasicText(mainAttr.id, mainAttr.value)
    local subName, subValue = TextHelper.getAttrBasicText(subAttr.id, subAttr.value)
    self._textMainAttr.text = mainName .. "+" .. mainValue
    self._textSubAttr.text = subName .. "+" .. subValue

    self._textName.text = config.name
    self._imageEquip.sprite = G_ResourceManager:loadSprite("EquipAltas", config.res)

    local level = self._unit:getLevel()
    local haveMoney = UserDataHelper.getNumByTypeValue(TypeHelper.TYPE_RESOURCE, DataConst.RES_MONEY)
    local costMoney, costItem = EquipDataHelper.getEquipmentUpCost(unit)
    local haveItem = UserDataHelper.getNumByTypeValue(costItem.type, costItem.value)

    self._costItem1:updateUI(costItem.type, costItem.value)
    self._costItem2:updateUI(costItem.type, costItem.value)
    self._textCostMoney1.text = Lang.get("lang_cost_money", {num = costMoney .. "/" .. haveMoney}) --costMoney
    self._textCostItem1.text = "x" .. costItem.size .. "/" .. haveItem
    self._textCostMoney2.text = Lang.get("lang_cost_money", {num = (costMoney * 5) .. "/" .. haveMoney})
    self._textCostItem2.text = "x" .. costItem.size * 5 .. "/" .. haveItem
end

function PopupEquipmentUp:onClose()
end

function PopupEquipmentUp:_onBtnOneClick()
    if self._unit:getLevel() >= self._unit:getConfig().lv_max then
        G_UIManager:showTip(Lang.get("level_is_to_max"))
        return
    end
    local ret = G_UserData:getEquipment():upgradeEquipment(self._unit:getId(), 1)
    if ret == G_ErrorCode.OK then
        G_UIManager:showTip(Lang.get("upgarde_success"))
    else
        G_UIManager:showTip(Lang.get("cost_not_enougth"))
    end
    self:updateUI(self._unit)
end

function PopupEquipmentUp:_onBtnFiveClick()
    if self._unit:getLevel() >= self._unit:getConfig().lv_max then
        G_UIManager:showTip(Lang.get("level_is_to_max"))
        return
    end
    local ret = G_UserData:getEquipment():upgradeEquipment(self._unit:getId(), 5)
    if ret == G_ErrorCode.OK then
        G_UIManager:showTip(Lang.get("upgarde_success"))
    else
        G_UIManager:showTip(Lang.get("cost_not_enougth"))
    end
    self:updateUI(self._unit)
end

function PopupEquipmentUp:_onBtnCloseClick()
    self:close()
end

return PopupEquipmentUp
